import express from 'express';
import helmet from 'helmet';
import cors from 'cors';
import bodyParser from 'body-parser'
import logger from './api/middlewares/logger.middleware';
import * as MySQLConnector from './database/connector';
import user from './routes/user.routes';
import appropriation from './routes/appropriation.routes';
import role from './routes/role.routes';
import specialist from './routes/specialist.routes';
import researcher from './routes/researcher.routes';
import announcement from './routes/announcement.routes';
import machine from './routes/machine.routes';
import machineAppointment from './routes/machine-appointment.routes';
import researchGroup from './routes/research-group.routes';
import project from './routes/project.routes';
import projectDeclare from './routes/project-declare.routes';
import projectChange from './routes/project-change.routes';
import permission from './routes/permission.routes';
import errorHandler from './api/middlewares/error-handler.middleware';
import passport from 'passport';
import initialPassport from './config/passport';
const app = express();

const port = process.env.PORT || 3001;

initialPassport(passport);

// 返回只解析url被编码过的请求体,而且只查看content-type请求头与type选项匹配的请求,并允许选择使用querystring库解析url编码
app.use(bodyParser.urlencoded({ extended: true }));
// 返回只解析JSON且只查看Content-Type请求头与type选项匹配的请求
app.use(bodyParser.json());

// passport 初始化
// initPassportAndSessions(app);
// passport 初始化
app.use(passport.initialize());

// enable all CORS request
app.use(cors());
app.use('/public', express.static('public'));

//在指定的路径上挂载指定的中间件函数:当请求的路径的基础部分与路径匹配时，中间件函数将执行。
app.use("/api/users", user);
app.use("/api/appropriations", appropriation);
app.use("/api/roles", role);
app.use("/api/specialists", specialist);
app.use("/api/researchers", researcher);
app.use("/api/announcements", announcement);
app.use("/api/machines", machine);
app.use("/api/machineappointments", machineAppointment);
app.use("/api/researchgroups", researchGroup);
app.use("/api/projects", project);
app.use("/api/projectdeclares", projectDeclare);
app.use("/api/projectchanges", projectChange);
app.use("/api/permissions", permission);

// serve static files
// app.use(express.static(path.join(__dirname, '../public')));

// adding set of security middlewares
app.use(helmet());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(logger);

app.use(errorHandler);

// create database pool
MySQLConnector.init();

//绑定并监听指定主机和端口上的连接。此方法与 Node 的http.Server.listen()相同。
app.listen(port, () => {
  console.log(`server running on port ${port}`);
});

